package com.harmontronics.hanma.paas.paas.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.harmontronics.hanma.paas.paas.common.PageBase;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import com.harmontronics.hanma.paas.paas.domain.app.entity.dictionary.HmDictionary;
import com.harmontronics.hanma.paas.paas.dto.input.HmDictionaryCreateInput;
import com.harmontronics.hanma.paas.paas.dto.input.HmDictionaryUpdateInput;
import com.harmontronics.hanma.paas.paas.dto.output.HmDictionaryResult;
import com.harmontronics.hanma.paas.paas.service.HmDictionaryAppService;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;


/**
 * 数据字典
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-05-31 15:07:30
 */
@RestController
@RequestMapping("/api/hmdictionary")
@Api(tags = {"数据字典"})
public class HmDictionaryController {

    @Autowired
    private HmDictionaryAppService hmDictionaryappService;

    /**
     * 分页列表
     */
    @GetMapping("/page/list")
    @ApiOperation("分页列表")
    public ResponseEntity<PageBase<HmDictionaryResult>> list(
            @ApiParam(value = "页码", defaultValue = "1") @RequestParam(required = false, defaultValue = "1") int pageNo,
            @ApiParam(value = "每页数据条数", defaultValue = "20") @RequestParam(required = false, defaultValue = "20") int pageSize,
            @ApiParam(value = "字典类型") @RequestParam(required = false) String type) {
        PageBase<HmDictionary> hmDictionaryPageBase = hmDictionaryappService
                .getList(pageNo, pageSize, type);
        PageBase<HmDictionaryResult> resultPageBase = PageBase
                .copy(hmDictionaryPageBase, HmDictionaryResult.class);
        return ResponseEntity.ok(resultPageBase);
    }


    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmDictionaryResult>> list() {
        List<HmDictionary> hmDictionaryList = hmDictionaryappService.getList();
        return ResponseEntity.ok(Convert.toList(HmDictionaryResult.class, hmDictionaryList));
    }

    @GetMapping("/type-list")
    @ApiOperation("列表")
    public ResponseEntity<List<String>> dictionaryTypeList() {
        List<HmDictionary> hmDictionaryList = hmDictionaryappService.getList();
        return ResponseEntity.ok(hmDictionaryList.stream().map(HmDictionary::getType).distinct()
                .collect(Collectors.toList()));
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation("保存")
    public ResponseEntity<String> save(
            @Valid @RequestBody HmDictionaryCreateInput hmDictionaryInput,
            BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmDictionary hmDictionary = BeanUtil.toBean(hmDictionaryInput, HmDictionary.class);
        hmDictionaryappService.save(hmDictionary);
        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(
            @Valid @RequestBody HmDictionaryUpdateInput hmDictionaryInput,
            BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmDictionary hmDictionary = BeanUtil.toBean(hmDictionaryInput, HmDictionary.class);
        hmDictionaryappService.update(hmDictionary);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
        hmDictionaryappService.remove(Arrays.asList(ids));
        return ResponseEntity.ok();
    }


    @GetMapping("/validate-field")
    @ApiOperation(value = "租户表单字段验证")
    @ApiIgnore
    public ResponseEntity<String> validateField(
            @ApiParam(name = "key", value = "key", required = true) @RequestParam String key,
            @ApiParam(name = "value", value = "value", required = true) @RequestParam String value,
            @ApiParam(name = "type", value = "type", required = true) @RequestParam String type,
            @ApiParam(name = "tenantId", value = "tenantId", required = true) @RequestParam Long tenantId) {
        hmDictionaryappService.validateField(key, value, type, tenantId);
        return ResponseEntity.ok();
    }



}